package com.black.common.mybatis.datascope;

import com.black.common.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author linwenfeng
 */
@Component
public class QueryDataScopeHandler {

    @Autowired
    private DataScopeHandlerInterceptorRegistry dataScopeHandlerInterceptorRegistry;

    public String handler(DataScope dataScope) throws Exception {
        if (dataScopeHandlerInterceptorRegistry != null) {
            List<DataScopeSqlHandler> registrations = dataScopeHandlerInterceptorRegistry.getRegistrations();
            for (DataScopeSqlHandler handlerInterceptor : registrations) {
                if (handlerInterceptor.getType().equals(dataScope.handlerType())) {
                    return handlerInterceptor.getScopeSql(dataScope);
                }
            }
        }
        return "";
    }

}
